<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('电表数据明细')" />
    <th:block th:include="include :: bootstrap-select-css" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="gray-bg">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>开始日期：</label>
                                <input type="text" class="time-input" id="startDate" placeholder="开始日期" name="startDate"/>
                            </li>
                            <li>
                                <label>结束日期：</label>
                                <input type="text" class="time-input" id="endDate" placeholder="结束日期" name="endDate"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="resetForm()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="exportExcel()" shiro:hasPermission="system:meterdata:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var prefix = ctx + "system/meterdata";
        
        // 自定义重置函数
        function resetForm() {
            // 先调用系统默认的重置方法
            $.form.reset();
            
            // 然后重新设置当天日期
            var today = new Date();
            var year = today.getFullYear();
            var month = (today.getMonth() + 1).toString().padStart(2, '0');
            var day = today.getDate().toString().padStart(2, '0');
            var dateStr = year + "-" + month + "-" + day;
            $("#startDate").val(dateStr);
            $("#endDate").val(dateStr);
            
            // 重新搜索以更新表格数据
            $.table.search();
        }

        // 导出Excel函数
        function exportExcel() {
            var startDate = $("#startDate").val();
            var endDate = $("#endDate").val();
            var url = prefix + "/exportDetail";
            $.modal.confirm("确定导出当前的电表数据明细吗？", function() {
                $.ajax({
                    type: "POST",
                    url: url,
                    data: {
                        "startDate": startDate,
                        "endDate": endDate
                    },
                    success: function(result) {
                        if (result.code == web_status.SUCCESS) {
                            window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                        } else {
                            $.modal.alertError(result.msg);
                        }
                    }
                });
            });
        }
        
        $(function() {
            // 设置当天日期为默认值
            var today = new Date();
            var year = today.getFullYear();
            var month = (today.getMonth() + 1).toString().padStart(2, '0');
            var day = today.getDate().toString().padStart(2, '0');
            var dateStr = year + "-" + month + "-" + day;
            $("#startDate").val(dateStr);
            $("#endDate").val(dateStr);
            
            var options = {
                url: prefix + "/dataDetail",
                sortName: "collectTime",
                sortOrder: "desc",
                modalName: "电表数据",
                queryParams: function(params) {
                    var search = {};
                    $.each($("#formId").serializeArray(), function(i, field) {
                        search[field.name] = field.value;
                    });
                    search.pageSize = params.limit;
                    search.pageNum = params.offset / params.limit + 1;
                    search.orderByColumn = params.sort;
                    search.isAsc = params.order;
                    return search;
                },
                columns: [
                    // 移除了checkbox列
                    {
                        field: 'id',
                        title: 'ID',
                        visible: false
                    },
                    {
                        field: 'deviceId',
                        title: '设备ID',
                        visible: false
                    },
                    {
                        field: 'collectTime',
                        title: '采集时间',
                        sortable: true
                    },
                    {
                        field: 'totalActiveEnergy',
                        title: '有功总电能(kWh)'
                    },
                    {
                        field: 'ua',
                        title: 'A相电压(V)'
                    },
                    {
                        field: 'ub',
                        title: 'B相电压(V)'
                    },
                    {
                        field: 'uc',
                        title: 'C相电压(V)'
                    },
                    {
                        field: 'uab',
                        title: 'AB线电压(V)',
                        visible: false
                    },
                    {
                        field: 'ubc',
                        title: 'BC线电压(V)',
                        visible: false
                    },
                    {
                        field: 'uca',
                        title: 'CA线电压(V)',
                        visible: false
                    },
                    {
                        field: 'ia',
                        title: 'A相电流(A)'
                    },
                    {
                        field: 'ib',
                        title: 'B相电流(A)'
                    },
                    {
                        field: 'ic',
                        title: 'C相电流(A)'
                    },
                    {
                        field: 'pt',
                        title: '总有功功率(kW)'
                    },
                    {
                        field: 'pf',
                        title: '总功率因数'
                    },
                    {
                        field: 'freq',
                        title: '频率(Hz)'
                    },
                    {
                        field: 'status',
                        title: '数据状态',
                        formatter: function(value, row, index) {
                            return value == 1 ? '<span class="label label-success">正常</span>' : 
                                '<span class="label label-danger">异常</span>';
                        }
                    }
                ]
            };
            $.table.init(options);
        });
    </script>
</body>
</html>